package com.chenjiagou.ordertickets.service.impl;

import com.chenjiagou.ordertickets.entity.Airport;
import com.chenjiagou.ordertickets.dao.AirportDao;
import com.chenjiagou.ordertickets.service.AirportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (Airport)表服务实现类
 *
 * @author makejava
 * @since 2025-06-23 14:54:15
 */
@Service("airportService")
public class AirportServiceImpl implements AirportService {
    @Resource
    private AirportDao airportDao;

    /**
     * 通过ID查询单条数据
     *
     * @param code 主键
     * @return 实例对象
     */
    @Override
    public Airport queryById(String code) {
        return this.airportDao.queryById(code);
    }


    /**
     * 新增数据
     *
     * @param airport 实例对象
     * @return 实例对象
     */
    @Override
    public Airport insert(Airport airport) {
        this.airportDao.insert(airport);
        return airport;
    }

    /**
     * 修改数据
     *
     * @param airport 实例对象
     * @return 实例对象
     */
    @Override
    public Airport update(Airport airport) {
        this.airportDao.update(airport);
        return this.queryById(airport.getCode());
    }

    /**
     * 通过主键删除数据
     *
     * @param code 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String code) {
        return this.airportDao.deleteById(code) > 0;
    }
    @Override
    public List<Airport> suggest(String keyword) {
        // JDK 11 的 String.isBlank() ➜ JDK 8 写法
        if (keyword == null || keyword.trim().isEmpty()) {
            return Collections.emptyList();   // JDK 9 的 List.of() ➜ JDK 8 写法
        }
        String trimmed = keyword.trim();

        return airportDao.searchAirports(trimmed);
    }
}
